{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "cb9e3f4a",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "[[-0.06260298 -0.05269521 -0.11279505  0.08031022 -0.17513186 -0.01382222\n",
      "  -0.05881321  0.11555742  0.0533998  -0.00455373  0.01423225  0.1367362\n",
      "  -0.03182742 -0.0804394   0.1295637  -0.10013699  0.00119168  0.17016934\n",
      "   0.2126241   0.02135233  0.07116281  0.23779617 -0.02037872  0.0825123\n",
      "  -0.00563384  0.2230357   0.00775988 -0.05716215  0.01548253  0.00424218\n",
      "  -0.04595441  0.0902499   0.10335156  0.106633   -0.10452288 -0.00707179\n",
      "  -0.22914346 -0.0936707  -0.17129396 -0.17977881  0.20126148  0.04047809\n",
      "  -0.04798479 -0.05677133 -0.06585852  0.17185852  0.07229134 -0.06497366\n",
      "  -0.01526989 -0.07283651  0.05664137 -0.19001459  0.1614005   0.01109679\n",
      "   0.17615786  0.10956986 -0.13153873  0.01154493  0.13526027 -0.11025011\n",
      "   0.16639951  0.01132257  0.10669625  0.12617227  0.08748352 -0.0359056\n",
      "   0.03229609  0.08252083 -0.1203839  -0.09342752 -0.08899727  0.10417508\n",
      "  -0.04251912 -0.02806914  0.0113694   0.13434428 -0.02241356  0.10680994\n",
      "  -0.00792967  0.115716   -0.08077454 -0.04558095  0.06389292  0.00714112\n",
      "   0.03623055  0.03364102  0.07366502 -0.12849566 -0.15800154  0.14128818\n",
      "   0.06722786 -0.07646551 -0.0261447   0.01535825  0.00754373 -0.00101883\n",
      "   0.03261608 -0.06094985 -0.09501901 -0.19570914]\n",
      " [-0.04408346 -0.0110703  -0.1907609  -0.08282605 -0.08101021 -0.19626002\n",
      "   0.1231699  -0.03307193 -0.18281566  0.17029469  0.09240631  0.03026365\n",
      "   0.10065925 -0.05403674  0.06351945  0.09770335 -0.03498415 -0.14127474\n",
      "  -0.0398009  -0.1296576  -0.18020092 -0.03407362  0.08024716 -0.02543468\n",
      "  -0.02665629 -0.12785517 -0.04505165  0.20872071  0.19693685  0.03901102\n",
      "   0.02771281 -0.11930584  0.01472993  0.03496747  0.06491011  0.05572348\n",
      "   0.12282015  0.06626408 -0.0639474  -0.00811742  0.0693016  -0.2174863\n",
      "   0.13385928  0.05982513 -0.09328569  0.01495596  0.01147153 -0.03010176\n",
      "  -0.12007959 -0.09256234  0.07710683 -0.14223126 -0.03166239 -0.0729901\n",
      "   0.08622141 -0.0541815  -0.11687803  0.01162091  0.10369512  0.10917702\n",
      "   0.12144385 -0.0641761  -0.03925911 -0.15502755  0.07951108  0.06157202\n",
      "  -0.13142332  0.0411906   0.06384446  0.11609496 -0.06506827  0.03120654\n",
      "  -0.05024901 -0.02793198  0.176136   -0.1183615  -0.19773512 -0.11345184\n",
      "   0.00941246  0.07025892 -0.10386522  0.01147875 -0.15195285 -0.1054963\n",
      "  -0.04569667  0.16161543  0.12436233  0.05022135  0.07178321  0.04858593\n",
      "  -0.05965259  0.13635619 -0.13775967 -0.061859   -0.07747477 -0.05254795\n",
      "   0.07851233 -0.02255103  0.06271427  0.1345256 ]\n",
      " [-0.15508121  0.01525386 -0.05903074 -0.14982755  0.0104712  -0.25059775\n",
      "  -0.12572864  0.03042585 -0.05459359  0.02010172  0.10865416 -0.04884174\n",
      "  -0.01893041  0.08336566 -0.12208471 -0.12512633 -0.09367463 -0.01261376\n",
      "  -0.05183262 -0.07985922 -0.07850557 -0.00573881 -0.01911884  0.0020043\n",
      "   0.0726632  -0.13830714  0.04799024  0.13142017  0.06983512 -0.02230063\n",
      "   0.10312728  0.08163138 -0.2502455   0.12150273  0.03459842 -0.20404008\n",
      "  -0.02743608 -0.04648662 -0.08917551 -0.07766759  0.00857664 -0.04518247\n",
      "  -0.08476535  0.13836576  0.04653631  0.04115129 -0.05447568 -0.08129998\n",
      "  -0.13448279  0.08328562 -0.01761593  0.09992457 -0.09285307 -0.06413532\n",
      "  -0.06462999  0.02193755 -0.01553635 -0.07135736 -0.24484347  0.11558755\n",
      "   0.05210398  0.10951477  0.04066571  0.01607698 -0.00871533 -0.00768387\n",
      "   0.10849708 -0.09945505 -0.01966882  0.03746945  0.12358439  0.25984755\n",
      "   0.29823527  0.02329573 -0.1122632   0.10460453  0.06343201 -0.02672722\n",
      "   0.05016563 -0.15105082  0.16903023 -0.1798234   0.00111998  0.04532235\n",
      "   0.10953998  0.14046434  0.07286349  0.19601075 -0.16323721 -0.05440436\n",
      "   0.17615178  0.058301    0.09402515  0.08106949  0.11283623  0.0563787\n",
      "  -0.0934933  -0.27500546 -0.15315844  0.00763162]\n",
      " [-0.20202355 -0.09584019 -0.05770147  0.11078042  0.00824772  0.00370021\n",
      "  -0.29210493 -0.0118224   0.0333748  -0.11722219 -0.17912738  0.04210884\n",
      "   0.08497082  0.04365069  0.01681738  0.0281403  -0.07460475 -0.04550583\n",
      "  -0.16332568  0.04160767 -0.04324061 -0.02847612  0.17654325  0.07422455\n",
      "  -0.03344378 -0.02430496  0.02781631  0.11563193 -0.03195404 -0.0295186\n",
      "   0.08610187 -0.05798585 -0.01556243  0.10067349  0.04271669 -0.0083863\n",
      "  -0.08787534 -0.00505074  0.00161748 -0.06004401  0.06213814  0.11521889\n",
      "   0.03860141 -0.01835651  0.07116465  0.11936637 -0.07330154  0.06070631\n",
      "  -0.16072349 -0.02012892 -0.07146882  0.00811579  0.09672617 -0.05473778\n",
      "   0.03906337  0.07797787 -0.06522544  0.03360568 -0.2045902  -0.08158368\n",
      "   0.03001272  0.01992918  0.06966378  0.20272887 -0.06279472 -0.14013891\n",
      "   0.20686343 -0.05060017  0.0402653  -0.03739551  0.04356883 -0.07287874\n",
      "  -0.03589477  0.03686896  0.21911803 -0.20390895  0.0282594  -0.00847587\n",
      "  -0.03174684  0.04464471  0.03801628  0.03477971  0.09476941  0.00555856\n",
      "   0.05114872  0.02576235  0.09887169  0.0464098  -0.10444441 -0.09181295\n",
      "  -0.02371629 -0.07843702 -0.01112923 -0.11020153  0.02642139  0.06990557\n",
      "   0.01923041 -0.07908712  0.13173376  0.11194455]]\n",
      "<NDArray 4x100 @cpu(0)>\n",
      "epoch 1,loss 196244.031250\n",
      "epoch 2,loss 191999.218750\n",
      "epoch 3,loss 191433.890625\n",
      "epoch 4,loss 193600.671875\n",
      "epoch 5,loss 181317.031250\n",
      "epoch 6,loss 178209.171875\n",
      "epoch 7,loss 175178.765625\n",
      "epoch 8,loss 171902.515625\n",
      "epoch 9,loss 169046.062500\n",
      "epoch 10,loss 171387.859375\n",
      "epoch 11,loss 164810.156250\n",
      "epoch 12,loss 169703.859375\n",
      "epoch 13,loss 159557.875000\n",
      "epoch 14,loss 157961.625000\n",
      "epoch 15,loss 174503.015625\n",
      "epoch 16,loss 153597.421875\n",
      "epoch 17,loss 154471.062500\n",
      "epoch 18,loss 151857.437500\n",
      "epoch 19,loss 152201.187500\n",
      "epoch 20,loss 172294.187500\n",
      "epoch 21,loss 148190.734375\n",
      "epoch 22,loss 146791.515625\n",
      "epoch 23,loss 145709.453125\n",
      "epoch 24,loss 153585.781250\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-97-55d4100dd8ad>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m    118\u001b[0m \u001b[0mtrain_iter\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata_iter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx_label\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    119\u001b[0m \u001b[0mtest_iter\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata_iter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_test\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_label\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 120\u001b[1;33m \u001b[0mtrain_ch3\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrain_iter\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_iter\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msquared_loss\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum_epochs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    121\u001b[0m \u001b[1;31m#set_figsize()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    122\u001b[0m \u001b[1;31m#plt.scatter(x[:, 1].asnumpy(), x_label[:, 0].asnumpy(), 1);  # 加分号只显示图\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-97-55d4100dd8ad>\u001b[0m in \u001b[0;36mtrain_ch3\u001b[1;34m(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr)\u001b[0m\n\u001b[0;32m    102\u001b[0m                 \u001b[0ml\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mloss\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_hat\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    103\u001b[0m             \u001b[0ml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m   \u001b[1;31m#求梯度\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 104\u001b[1;33m             \u001b[0msgd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[1;33m)\u001b[0m    \u001b[1;31m#更新wb权重\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    105\u001b[0m \u001b[1;31m#         print(params)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    106\u001b[0m         \u001b[0mtrain_l_sum\u001b[0m \u001b[1;33m=\u001b[0m\u001b[0mloss\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx_label\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#误差\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-97-55d4100dd8ad>\u001b[0m in \u001b[0;36msgd\u001b[1;34m(params, lr, batch_size)\u001b[0m\n\u001b[0;32m     84\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msgd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     85\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mparam\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 86\u001b[1;33m         \u001b[0mparam\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparam\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mlr\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mparam\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgrad\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     87\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     88\u001b[0m def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,\n",
      "\u001b[1;32mD:\\conda\\envs\\gluon\\lib\\site-packages\\mxnet\\ndarray\\ndarray.py\u001b[0m in \u001b[0;36mgrad\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   2177\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mhdl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2178\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2179\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0m_ndarray_cls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhdl\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2180\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2181\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mdetach\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\conda\\envs\\gluon\\lib\\site-packages\\mxnet\\ndarray\\sparse.py\u001b[0m in \u001b[0;36m_ndarray_cls\u001b[1;34m(handle, writable, stype)\u001b[0m\n\u001b[0;32m   1177\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_ndarray_cls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwritable\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0m_STORAGE_TYPE_UNDEFINED\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1178\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mstype\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0m_STORAGE_TYPE_UNDEFINED\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1179\u001b[1;33m         \u001b[0mstype\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_storage_type\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1180\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mstype\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0m_STORAGE_TYPE_DEFAULT\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1181\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mNDArray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwritable\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mwritable\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\conda\\envs\\gluon\\lib\\site-packages\\mxnet\\ndarray\\ndarray.py\u001b[0m in \u001b[0;36m_storage_type\u001b[1;34m(handle)\u001b[0m\n\u001b[0;32m    169\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_storage_type\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    170\u001b[0m     \u001b[0mstorage_type\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mc_int\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 171\u001b[1;33m     \u001b[0mcheck_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_LIB\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMXNDArrayGetStorageType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mctypes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbyref\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstorage_type\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    172\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mstorage_type\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    173\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import d2lzh as d2l\n",
    "import xlrd\n",
    "import random\n",
    "import math\n",
    "from IPython import display\n",
    "from matplotlib import pyplot as plt\n",
    "from mxnet import autograd, nd\n",
    "batch_size =1\n",
    "num_inputs = 4\n",
    "num_outputs = 1\n",
    "num_hiddens=100\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "w = nd.random.normal(scale=0.1, shape=(num_inputs, num_hiddens))\n",
    "b = nd.zeros(num_hiddens)\n",
    "w1=nd.random.normal(scale=0.1, shape=(num_hiddens, num_outputs))\n",
    "b1= nd.zeros(num_outputs)\n",
    "\n",
    "w.attach_grad()\n",
    "b.attach_grad()\n",
    "w1.attach_grad()\n",
    "b1.attach_grad()\n",
    "\n",
    "params=[w,b,w1,b1]\n",
    "print(w)\n",
    "def use_svg_display():\n",
    "    # 用矢量图显示\n",
    "    display.set_matplotlib_formats('svg')\n",
    "\n",
    "def set_figsize(figsize=(3.5, 2.5)):\n",
    "    use_svg_display()\n",
    "    # 设置图的尺寸\n",
    "    plt.rcParams['figure.figsize'] = figsize\n",
    "\n",
    "def squared_loss(y_hat, y):\n",
    "    return (y_hat - y) ** 2 / batch_size\n",
    "\n",
    "def relu(X):\n",
    "    return nd.maximum(X,0)\n",
    "\n",
    "def net(X):\n",
    "    H=relu(nd.dot(X,w)+b)\n",
    "    Y=nd.dot(H, w1) + b1\n",
    "    return Y\n",
    "\n",
    "def excel2matrix(path):\n",
    "    data = xlrd.open_workbook(path)\n",
    "    table = data.sheets()[0]\n",
    "    nrows = table.nrows  # 行数\n",
    "    ncols = table.ncols  # 列数\n",
    "    datamatrix = nd.random.normal(scale=1,shape=(nrows, ncols))\n",
    "    for i in range(nrows):\n",
    "        rows = table.row_values(i)\n",
    "        datamatrix[i,:] = rows\n",
    "    return datamatrix\n",
    " \n",
    "def data_iter(batch_size, features, labels):\n",
    "    num_examples = len(features)\n",
    "    indices = list(range(num_examples))\n",
    "    random.shuffle(indices)  # 样本的读取顺序是随机的\n",
    "    for i in range(0, num_examples, batch_size):\n",
    "        j = nd.array(indices[i: min(i + batch_size, num_examples)])\n",
    "        yield features.take(j), labels.take(j)  # take函数根据索引返回对应元素\n",
    "# def cross_entropy(y_hat, y):\n",
    "#     return -nd.pick(y_hat, y).log()\n",
    "# def accuracy(y_hat, y):\n",
    "#     return (y_hat.argmax(axis=1) == y.astype('float32')).mean().asscalar()\n",
    "\n",
    "# def evaluate_accuracy(data_iter, net):\n",
    "#     acc_sum, n = 0.0, 0\n",
    "#     for X, y in data_iter:\n",
    "#         y = y.astype('float32')\n",
    "#         acc_sum += (net(X).argmax(axis=1) == y).sum().asscalar()\n",
    "#         n += y.size\n",
    "#     return acc_sum / n\n",
    "\n",
    "num_epochs, lr = 1000, 0.000001\n",
    "\n",
    "def sgd(params, lr, batch_size):  \n",
    "    for param in params:\n",
    "        param[:] = param - lr * param.grad / 2\n",
    "\n",
    "def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,\n",
    "              params=None, lr=None):\n",
    "    for epoch in range(num_epochs):\n",
    "        for X, y in data_iter(batch_size,x,x_label):\n",
    "            with autograd.record():\n",
    "                y_hat = net(X)\n",
    "#                 print('X')\n",
    "#                 print(X)\n",
    "#                 print('y_hat')\n",
    "#                 print(y_hat)\n",
    "#                 print('y')\n",
    "#                 print(y)\n",
    "#                 print('[W,b]')\n",
    "#                 print([w,b])\n",
    "                l = loss(y_hat, y)\n",
    "            l.backward()   #求梯度\n",
    "            sgd(params, lr, batch_size)    #更新wb权重   \n",
    "#         print(params)\n",
    "        train_l_sum =loss(net(x),x_label)  #误差\n",
    "        print('epoch %d,loss %f' % (epoch + 1,train_l_sum.mean().asnumpy()))\n",
    "\n",
    "\n",
    "pathX = '309.xls'  #  113.xlsx 在当前文件夹下\n",
    "pathX2 = '309_label.xls'  #  113.xlsx 在当前文件夹下\n",
    "pathX3 = '309_pre.xls'  #  113.xlsx 在当前文件夹下\n",
    "x = excel2matrix(pathX)\n",
    "x_label=excel2matrix(pathX2)\n",
    "y_test=excel2matrix(pathX3)\n",
    "y_label=nd.zeros((y_test.shape[0],1))\n",
    "\n",
    "train_iter=data_iter(batch_size,x,x_label)\n",
    "test_iter=data_iter(batch_size,y_test,y_label)\n",
    "train_ch3(net, train_iter, test_iter, squared_loss, num_epochs, batch_size,params, lr)\n",
    "#set_figsize()\n",
    "#plt.scatter(x[:, 1].asnumpy(), x_label[:, 0].asnumpy(), 1);  # 加分号只显示图\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "e86025e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "[[ 2.12144542e+00 -1.12421967e-01  2.80527973e+00  1.30053222e+00\n",
      "   3.32869813e-02  5.26635349e-02  8.91986370e-01 -7.32067823e-02\n",
      "  -6.68723360e-02 -1.41183585e-02 -2.21818797e-02  9.60046649e-01\n",
      "   2.62109727e-01  5.59708588e-02  4.70007863e-03  4.15559709e-01\n",
      "   1.39308298e+00  5.38688660e-01  6.83365881e-01 -7.50383884e-02\n",
      "  -1.35027178e-04 -9.72329974e-02 -4.66808304e-02  1.54586065e+00\n",
      "  -1.13417506e-01 -1.56707481e-01  1.64502293e-01  3.68836105e-01\n",
      "  -1.36346430e-01  1.67838955e+00 -2.20497977e-03  9.13706720e-01\n",
      "   1.72232866e+00 -8.36656690e-02  7.74190128e-01 -6.51923567e-02\n",
      "  -1.89337209e-02 -1.03309751e-01  3.90142500e-01 -6.27104342e-02\n",
      "  -1.84289083e-01 -1.28444865e-01 -5.24386615e-02 -1.40566334e-01\n",
      "   9.38634574e-01  8.58613253e-01 -1.01968087e-01 -1.61448047e-02\n",
      "   2.66311073e+00  7.18165860e-02  7.16158450e-02 -9.85292122e-02\n",
      "  -5.28374650e-02 -1.61109924e-01 -5.81675535e-03  5.22835031e-02\n",
      "  -2.87759639e-02 -4.45923992e-02  1.37882841e+00 -9.84044094e-03\n",
      "   1.50752813e-02  1.49951279e+00  2.34884429e+00 -1.01711459e-01\n",
      "  -1.96231157e-01 -1.38113305e-01  1.65698886e+00  5.66088974e-01\n",
      "   2.92338222e-01  1.72745514e+00 -9.45870131e-02  1.45404283e-02\n",
      "  -1.82685256e-02  1.78324747e+00 -1.55195475e-01  8.37454557e-01\n",
      "   1.59640026e+00 -3.57423984e-02  9.23951250e-03 -1.41450129e-02\n",
      "  -4.41192724e-02 -7.12451637e-02 -1.70142092e-02 -6.65039048e-02\n",
      "   1.51524348e-02  2.67792139e-02 -7.64777809e-02  2.20051885e-01\n",
      "  -5.94714470e-02  9.14319217e-01  2.89611667e-01  4.88339573e-01\n",
      "  -7.86822010e-03  6.23883121e-02  7.51055479e-02  7.65177682e-02\n",
      "  -5.71599379e-02 -5.40612414e-02  3.93524313e+00  4.06244904e-01]\n",
      " [ 1.71124384e-01 -5.44660687e-02  1.74988061e-02  6.08570166e-02\n",
      "  -4.06620316e-02  1.16187027e-02  1.94897637e-01  3.01148649e-02\n",
      "  -1.25609085e-01  5.72898649e-02  3.11141815e-02  1.80106552e-03\n",
      "   1.12574235e-01  2.94371117e-02 -1.50440499e-01 -1.78855181e-01\n",
      "   1.45425886e-01  7.41777048e-02  2.06301332e-01  1.32565558e-01\n",
      "   1.42854229e-01  9.87464115e-02  2.59652678e-02  3.25891301e-02\n",
      "  -1.54511184e-01  1.02979995e-01  1.34215951e-01  1.61119819e-01\n",
      "  -1.34293304e-03  1.31890580e-01 -1.16413273e-01  2.80429900e-01\n",
      "   1.85453773e-01  9.64511931e-03  1.25065699e-01  1.52327672e-01\n",
      "   1.19156055e-01  1.00794122e-01  1.16006792e-01  1.39727160e-01\n",
      "  -9.13567543e-02  1.31444082e-01  6.88255951e-03 -4.96293418e-02\n",
      "   2.68193334e-01  1.40722737e-01  1.05275802e-01  3.98326479e-02\n",
      "   1.27377927e-01  1.01370998e-02 -3.13880593e-02  1.05004609e-01\n",
      "  -6.80276453e-02  1.04184665e-01  3.69920582e-02 -4.90196608e-02\n",
      "  -6.26619905e-02 -9.98187438e-02  3.25185023e-02  6.22853003e-02\n",
      "  -8.18045959e-02  3.64120789e-02  1.66573733e-01 -1.13761649e-01\n",
      "   8.74518678e-02 -4.05002199e-02  1.68513685e-01 -6.35811016e-02\n",
      "  -3.80872823e-02  4.46682572e-02 -5.94875291e-02  1.07755221e-01\n",
      "  -1.89769603e-02  7.91106001e-02  5.07554114e-02 -9.20292959e-02\n",
      "  -6.76396713e-02  1.07571200e-01 -1.28019914e-01 -1.04904890e-01\n",
      "   1.85735509e-01 -8.70084092e-02  1.17085941e-01 -3.55185964e-03\n",
      "  -1.33855939e-01  9.50527843e-04  7.00514242e-02 -7.57596940e-02\n",
      "   1.05701342e-01  3.13806921e-01  2.11302526e-02  1.44721031e-01\n",
      "  -6.74126893e-02 -9.64369699e-02 -9.05895159e-02 -2.23579761e-02\n",
      "  -4.86797579e-02 -6.90940991e-02  2.25388527e-01  6.56824000e-03]\n",
      " [ 2.61538529e+00  8.17010328e-02  3.30572820e+00  1.47914720e+00\n",
      "  -6.70386925e-02 -4.85558920e-02  1.35220170e+00 -3.84126976e-02\n",
      "  -1.17612258e-01 -1.31722555e-01 -8.25526484e-04  1.17700827e+00\n",
      "   3.03138703e-01 -5.08974120e-02  4.73889895e-03  5.74419796e-01\n",
      "   1.77620316e+00  7.90595114e-01  8.84706438e-01 -4.38107587e-02\n",
      "  -1.63972884e-01  2.34506559e-02 -2.20601130e-02  1.66911435e+00\n",
      "  -7.07021356e-02 -1.29128084e-01  7.24977851e-02  3.74544799e-01\n",
      "   4.65802662e-02  1.90532875e+00 -1.94562554e-01  9.00538802e-01\n",
      "   2.24697542e+00  1.28971133e-02  7.99459219e-01  2.16329917e-02\n",
      "  -1.02161884e-03  8.27890560e-02  5.16510248e-01  4.31409515e-02\n",
      "  -9.77655780e-03 -1.78385023e-02  1.49259716e-01 -1.39514275e-03\n",
      "   1.22063911e+00  9.54513907e-01  3.46742310e-02 -1.44463405e-01\n",
      "   3.45716953e+00  6.39049616e-03  1.87553242e-01  6.34129792e-02\n",
      "   5.91074489e-02  3.31370309e-02 -3.46940979e-02 -1.46745995e-01\n",
      "  -8.06528702e-02  4.00631912e-02  1.85412419e+00 -8.35651085e-02\n",
      "  -3.66885774e-02  1.91093731e+00  2.92884111e+00  9.44124833e-02\n",
      "  -2.32244283e-01  1.39173388e-01  2.20259809e+00  7.04795241e-01\n",
      "   2.71307707e-01  2.02830219e+00 -1.16152838e-01 -1.27722055e-01\n",
      "  -1.97629398e-03  2.18071389e+00 -5.20482436e-02  7.74259031e-01\n",
      "   2.00739145e+00  4.11253572e-02  1.84638038e-01  1.83734354e-02\n",
      "  -6.53150231e-02 -2.63896912e-01  1.26623902e-02  6.35275543e-02\n",
      "  -5.13067022e-02 -1.96465194e-01  3.94170843e-02  2.13671908e-01\n",
      "   5.56552596e-02  9.47925806e-01  4.34581757e-01  7.33206689e-01\n",
      "   1.97982020e-03 -5.92526942e-02 -7.02302009e-02 -8.09590071e-02\n",
      "   6.34951219e-02 -2.61512306e-02  4.74979496e+00  4.56200510e-01]\n",
      " [ 6.13986671e-01 -4.41392185e-03  1.05598390e+00  5.23531735e-01\n",
      "   4.61939387e-02 -1.04802161e-01  2.66653180e-01  8.01958665e-02\n",
      "  -2.36167330e-02 -1.43737197e-01  7.62671009e-02  3.02740812e-01\n",
      "   8.21854174e-02 -1.56393394e-01 -9.00019333e-02 -3.39651592e-02\n",
      "   4.00090396e-01  3.08488697e-01  5.40658347e-02  6.86339065e-02\n",
      "  -1.59848228e-01  3.96966413e-02  6.36975989e-02  4.76326138e-01\n",
      "   2.14037728e-02 -6.90337420e-02  9.11549479e-03  1.68682620e-01\n",
      "   1.15760947e-02  6.70909047e-01  1.49569154e-01  1.80661216e-01\n",
      "   4.79104668e-01 -1.11971930e-01  2.44716585e-01  1.05685383e-01\n",
      "   4.82850075e-02 -1.53478542e-02  1.29740804e-01  4.52609453e-03\n",
      "  -1.05840757e-01  1.35661721e-01 -1.24510638e-01 -5.29721491e-02\n",
      "   4.26750392e-01  3.06340128e-01 -3.58172841e-02  1.70229509e-01\n",
      "   8.19710970e-01 -9.63382982e-03  6.98268116e-02 -4.58782576e-02\n",
      "  -9.26640555e-02 -9.52759981e-02  1.57906041e-01 -1.11883609e-02\n",
      "  -5.49345613e-02  8.57346691e-03  2.78340131e-01  3.28741372e-02\n",
      "  -6.16274849e-02  4.79702741e-01  8.11853588e-01 -2.17634905e-02\n",
      "  -3.36088948e-02 -1.04599491e-01  5.18758774e-01  1.83972955e-01\n",
      "   8.80729482e-02  5.74770868e-01 -5.94495190e-03 -1.13954000e-01\n",
      "  -2.46750861e-02  5.72666228e-01  1.06909826e-01  3.05763900e-01\n",
      "   4.86068904e-01 -2.16071650e-01  5.22785075e-03 -1.65034309e-02\n",
      "   2.24884972e-02 -4.68673781e-02 -3.34788524e-02  6.06094599e-02\n",
      "   8.88478234e-02 -1.28810853e-01 -5.45553379e-02  1.33243501e-01\n",
      "  -2.25794181e-01  9.74064842e-02  1.16190851e-01  3.80333513e-01\n",
      "   3.46639007e-02  2.22766660e-02 -3.91390026e-02 -5.71087077e-02\n",
      "  -7.62712434e-02  1.50223881e-01  1.32037795e+00  4.66251858e-02]]\n",
      "<NDArray 4x100 @cpu(0)>, \n",
      "[ 3.4111550e-01  0.0000000e+00  4.4248140e-01  1.9776504e-01\n",
      "  0.0000000e+00 -3.0140537e-03  1.6424531e-01  0.0000000e+00\n",
      "  0.0000000e+00  0.0000000e+00 -7.5755338e-03  1.5773687e-01\n",
      "  3.0477656e-02 -2.3221080e-04 -4.9549304e-03  6.4497314e-02\n",
      "  2.2599527e-01  9.5454849e-02  1.0982796e-01  0.0000000e+00\n",
      "  0.0000000e+00  0.0000000e+00  0.0000000e+00  2.1782421e-01\n",
      "  0.0000000e+00  0.0000000e+00  1.1484136e-02  5.6175239e-02\n",
      "  0.0000000e+00  2.6384410e-01  0.0000000e+00  1.3253641e-01\n",
      "  2.8741094e-01  0.0000000e+00  1.0627330e-01 -1.3637766e-02\n",
      " -9.7630396e-03 -2.7884522e-03  5.5751614e-02 -3.6953613e-03\n",
      "  0.0000000e+00  0.0000000e+00  5.9336731e-03  0.0000000e+00\n",
      "  1.5962745e-01  1.2572923e-01  0.0000000e+00  0.0000000e+00\n",
      "  4.4350779e-01  1.1472249e-03  1.5596322e-02  0.0000000e+00\n",
      " -3.3920549e-03  0.0000000e+00 -2.3534473e-03  0.0000000e+00\n",
      "  0.0000000e+00 -5.2934345e-03  2.3273380e-01  0.0000000e+00\n",
      "  0.0000000e+00  2.5217679e-01  3.8939235e-01 -4.9003344e-03\n",
      "  0.0000000e+00 -8.3547708e-04  2.8525358e-01  8.1261531e-02\n",
      "  3.2072973e-02  2.6462209e-01  0.0000000e+00  0.0000000e+00\n",
      "  0.0000000e+00  2.8476620e-01  0.0000000e+00  1.1760283e-01\n",
      "  2.5608239e-01 -4.3087341e-03  1.3847141e-02 -1.9351237e-04\n",
      "  0.0000000e+00  0.0000000e+00 -1.4470806e-03 -5.2183643e-03\n",
      "  0.0000000e+00  0.0000000e+00  0.0000000e+00  2.3151383e-02\n",
      "  0.0000000e+00  1.3463284e-01  5.2625503e-02  8.2259357e-02\n",
      " -2.9753190e-03 -3.2554020e-03 -9.7901757e-05  0.0000000e+00\n",
      " -1.4050625e-02  0.0000000e+00  6.3289160e-01  6.1440546e-02]\n",
      "<NDArray 100 @cpu(0)>]\n"
     ]
    }
   ],
   "source": [
    "print([w,b])\n",
    "a=net(y_test)\n",
    "\n",
    "set_figsize()\n",
    "#plt.scatter(x[:, 1].asnumpy(), x_label[:, 0].asnumpy(), 1);  # 加分号只显示图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "77210598",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2044.5299]\n",
      " [2044.5299]\n",
      " [2044.5299]\n",
      " ...\n",
      " [2089.627 ]\n",
      " [2089.627 ]\n",
      " [2092.1584]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a1=a.asnumpy()\n",
    "print(a1)\n",
    "np.savetxt(\"./result.txt\",a1,fmt='%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acaae3b5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:gluon] *",
   "language": "python",
   "name": "conda-env-gluon-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
